package swing.frame.whiteboard;

import java.awt.*;

public class Shape {

    public final static int LINE_TYPE = 1;
    public final static int OVAL_TYPE = 2;
    public final static int RECT_TYPE = 3;
    public final static int ROUND_RECT_TYPE = 4;
    public final static int Fillrect_RECT_TYPE = 5;
    public final static int TEXT_TYPE = 6;

    public int shape_type;
    public int x1, y1, x2, y2;//绘制图形的坐标
    public Color color;//画笔颜色
    public int width;//画笔粗细
    //子类新增属性，圆角矩形的角的弯曲程度
    public int arcWidth;
    public int arcHeight;
    public String text="";
    public String user="";

    /**
     * Line, Oval, Rect的构造方法
     */
    public Shape(int shape_type, int x1,int y1,int x2,int y2,Color color,int width){
        this.shape_type = shape_type;
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
        this.color=color;
        this.width=width;
    }


    @Override
    public String toString() {
        return shape_type + "," + x1 + "," + y1 + "," + x2 + "," + y2 + "," +
                color.getRed() + "," + color.getGreen() + "," + color.getBlue() + "," + width + "," +
                arcWidth + "," + arcHeight + "," + text;
    }


    /**
     * RoundRect的构造方法
     */
    public Shape(int shape_type, int x1,int y1,int x2,int y2,int i,int j,Color color ,int width){
        this.shape_type = shape_type;
        this.x1=x1;
        this.y1=y1;
        this.x2=x2;
        this.y2=y2;
        this.color=color;
        this.width=width;
        this.arcWidth=i;
        this.arcHeight=j;
    }

    /**
     * 文本类型
     */
    public Shape(int shape_type, int x1,int y1,Color color ,String text) {
        this.shape_type = shape_type;
        this.x1=x1;
        this.y1=y1;
        this.color=color;
        this.text=text;
    }

    public Shape(String shapeString) {
        String[] strs = shapeString.split(",");
        this.shape_type = Integer.valueOf(strs[0]);
        this.x1=Integer.valueOf(strs[1]);
        this.y1=Integer.valueOf(strs[2]);
        this.x2=Integer.valueOf(strs[3]);
        this.y2=Integer.valueOf(strs[4]);
        this.color=new Color(Integer.valueOf(strs[5]), Integer.valueOf(strs[6]), Integer.valueOf(strs[7]));
        this.width=Integer.valueOf(strs[8]);
        this.arcWidth=Integer.valueOf(strs[9]);
        this.arcHeight=Integer.valueOf(strs[10]);
        if (strs.length > 11) {
            this.text=strs[11];
        }
        if (strs.length > 12) {
            this.user = strs[12];
        }
    }

    public void Draw(Graphics2D g) {
        switch (shape_type) {
            case LINE_TYPE:
                DrawLine(g);
                break;
            case OVAL_TYPE:
                DrawOval(g);
                break;
            case RECT_TYPE:
                DrawRect(g);
                break;
            case ROUND_RECT_TYPE:
                DrawRoundRect(g);
                break;
            case Fillrect_RECT_TYPE:
                DrawFillRect(g);
                break;
            case TEXT_TYPE :
                DrawString(g);
                break;
        }
    }

    private void DrawString(Graphics2D g) {
        g.setFont(new Font("宋体",Font.PLAIN,18));
        g.setColor(this.color);
        g.drawString(text,x1,y1);
    }

    private void DrawLine(Graphics2D g) {
        g.setColor(this.color);
        g.setStroke(new BasicStroke(width));
        try {
            g.drawLine(x1, y1, x2, y2);
        } catch (Exception e) {
        }
    }

    private void DrawOval(Graphics2D g) {
        g.setColor(this.color);
        g.setStroke(new BasicStroke(width));
        g.drawOval(x1, y1, x2, y2);
    }

    private void DrawRect(Graphics2D g) {
        g.setColor(this.color);
        g.setStroke(new BasicStroke(width));
        g.drawRect(x1, y1, x2, y2);
    }

    private void DrawRoundRect(Graphics2D g) {
        g.setColor(this.color);
        g.setStroke(new BasicStroke(width));
        g.drawRoundRect(x1, y1, x2, y2, this.arcWidth, this.arcHeight);
    }
    private void DrawFillRect(Graphics2D g) {
        g.setColor(this.color);
        g.setStroke(new BasicStroke(width));
        g.fillRect(x1, y1, x2, y2);
    }
    private void DrawClear(Graphics2D g){
//        g.setColor(this.color);
        //g.setStroke(new BasicStroke(width));
        g.clearRect(0,0,1100,800);
    }

    public int getShape_type() {
        return shape_type;
    }

    public void setShape_type(int shape_type) {
        this.shape_type = shape_type;
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public int getX2() {
        return x2;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public int getY2() {
        return y2;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getArcWidth() {
        return arcWidth;
    }

    public void setArcWidth(int arcWidth) {
        this.arcWidth = arcWidth;
    }

    public int getArcHeight() {
        return arcHeight;
    }

    public void setArcHeight(int arcHeight) {
        this.arcHeight = arcHeight;
    }
}
